<template>
  <div class="m-launch">
    <!-- 搜索条件 -->
    <div class="launch-search">
      <div class="o">
        <span class="title">商家名称</span>
        <div class="input dis-block">
          <w-input holder="请填写" v-model="params.company"></w-input>
        </div>
      </div>
      <div class="o">
        <span class="title">商品名称</span>
        <div class="input dis-block">
          <w-input holder="请填写" v-model="params.goodsName"></w-input>
        </div>
      </div>
      <div class="o">
        <span class="title">商品货号</span>
        <div class="input dis-block">
          <w-input holder="请填写" v-model="params.goodsSerial"></w-input>
        </div>
      </div>
      <div class="o">
        <span class="title">商家手机号</span>
        <div class="input dis-block">
          <w-input holder="请填写" rule="isMobile" error="请输入正确的手机号" v-model="params.contactPhone"></w-input>
        </div>
      </div>
      <div class="o">
        <span class="title">供货方式</span>
        <div class="o-select dis-block">
          <w-select :options="supplyModeOptions" v-model="params.supplyMode"></w-select>
        </div>
      </div>

      <div class="search-btn">
        <w-button @on-click="fetchCommonGoodsSearch">查询</w-button>
      </div>
    </div>
    <!-- 商品列表 -->
    <div class="m-goods-list clearfix">
      <div class="tab-com">
        <w-table :columns="columns"></w-table>
      </div>
      <!-- 列表 -->
      <div v-if="goodsList.recordList && goodsList.recordList.length">
        <div class="table-list" v-for="(list, index) in goodsList.recordList" :key="index">
          <div class="tab-title">
            <!-- <span>商家ID：A12423</span> -->
            <span>商家名称：{{list.company}}</span>
          </div>
          <div>
            <table class="table">
              <tr>
                <td>
                  <div class="goods-info clearfix">
                    <div class="goods-img fl">
                      <img v-lazy="list.goodsMainPhotoPath" alt="">
                    </div>
                    <div class="goods-name fl">
                      <p class="p-line-hide">{{list.goodsName}}</p>
                      <span>货号：{{list.goodsSerial}}</span>
                    </div>
                  </div>
                </td>
                <td>
                  <span>{{list.contactPhone}}</span>
                </td>
                <td>
                  <span>{{list.goodsInventory || 0}}件</span>
                </td>
                <td>
                  <span>{{list.supplyMode | supplyMode}}</span>
                </td>
                <td>
                  <span>{{list.tierdPrice | goodsPrice}}</span>
                  <br>
                  <span>（出厂价：¥{{(list.factoryPrice || 0) | currency}}）</span>
                </td>
                <td>
                  <span>{{list.consignor | consignor}}</span>
                </td>
                <td>
                  <div class="t-under" @click="onSetLaunchGoods(list.id, list.goodsInventoryDetail)">设置拼单</div>
                </td>
              </tr>
            </table>
          </div>
        </div>
      </div>
      <div style="text-align: center" v-else>
        <p>暂无可拼单商品</p>
      </div>
      <!-- 分页 -->
      <div class="fr" v-if="goodsList.pageCount > 1">
        <w-paging :current="goodsList.currentPage" :total="goodsList.pageCount" @on-page="onChangePage"></w-paging>
      </div>
    </div>

    <!-- 设置拼单 -->
    <w-panel-validate title="设置拼单规则" :panel-title="true" confirmText="提交审核" v-model="setLaunchPanel" :height="`600px`" @on-confirm="submitUpdatedGoodsSkuInfoHandler" @on-cancel="_cancelParams">
      <div class="fightSet">
        <div class="fightRow">
          <div class="title">拼单结束时间：</div>
          <div class="date">
            <w-date-picker language="ch" :min="localDate" v-model="addParams.fightGoodsEndTime"></w-date-picker>
          </div>
        </div>
        <div class="fightRow">
          <div class="title">商品类型：</div>
          <div class="radioGroup clearfix">
            <div class="radio fl">
              <w-radio :label="`1`" name="goodsNumType" v-model="addParams.goodsNumType">现货</w-radio>
            </div>
            <!--  若该商品不是现货商品，则不可设置拼单商品为现货 -->
            <div class="radio">
              <w-radio :label="`0`" name="goodsNumType" v-model="addParams.goodsNumType">期货</w-radio>
            </div>
          </div>
        </div>
        <div class="fightRow">拼单参数配置</div>
        <div class="fightRow">
          <div class="SKU">
            <table class="table">
              <thead>
                <th>颜色</th><th>颜色最低发货量</th><th>尺码</th><th>采购商最低起订量</th><th v-if="addParams.goodsNumType == '1'">商品数量</th>
              </thead>
              <!-- SKU组合 -->
              <sku-group v-for="(item, index) in addParams.goodsRulesDetailList" :goods-type="addParams.goodsNumType" :index="index" :key="index" :specs="item" :count="true"></sku-group>
            </table>
          </div>
        </div>
        <div class="fightRow">
          <div class="title">拼单发货总量：</div>
          <div class="date">
            <div class="launch-count">
              <w-input rule="isEmpty|onlyIntNo0"  error="必填|格式错误" v-model="addParams.crowdfundingCount"></w-input>
            </div>
            件
          </div>
        </div>
      </div>
    </w-panel-validate>

  </div>
</template>

<script>
import SkuGroup from './components/SkuGroup'
import { apiGoodsMange } from 'js/api'
import { mapGetters } from 'vuex'
import { formatDate } from 'js/util'

export default {
  data() {
    return {
      columns: [
        {title: '货品', width: '28%'},
        {title: '手机号', width: '14%'},
        {title: '库存', width: '12%'},
        {title: '供货方式', width: '11%'},
        {title: '价格', width: '15%'},
        {title: '发货方', width: '10%'},
        {title: '操作', width: '10%'}
      ],
      setLaunchPanel: false, // 设置拼单弹框
      goodsRulesDetailList: [
        {
          specpropertyName: '黑色',
          specpropertyList: [
            {
              colorCode: '#ffffff',
              specpropertyId: '10',
              specpropertyInventCount: '20',
              specpropertyName: 'S',
              specpropertySerial: 'WRYXE141'
            },
            {
              colorCode: '#ffffff',
              specpropertyId: '10',
              specpropertyInventCount: '20',
              specpropertyName: 'M',
              specpropertySerial: 'WRYXE141'
            }
          ]
        }
      ],
      // 商品类型
      supplyModeOptions: [
        {value: '1', label: '现货'},
        {value: '0', label: '订货'}
      ],
      params: {
        accessToken: '',
        threeMenuCode: this.$route.query.three || '',
        currentPage: 1,
        showCount: 10,
        goodsStatus: 0, // 商品状态
        company: '',
        contactPhone: '',
        goodsName: '',
        goodsSerial: '',
        supplyMode: ''
      },
      goodsList: {},
      addParams: {
        accessToken: '',
        threeMenuCode: this.$route.query.three || '',
        id: '',
        fightGoodsEndTime: '',
        crowdfundingCount: '',
        goodsNumType: 1,
        goodsRulesDetailList: []
      }
    }
  },
  components: {
    'sku-group': SkuGroup
  },
  computed: {
    ...mapGetters(['token']),
    localDate() {
      return formatDate(new Date(), 'yyyy-MM-dd')
    }
  },
  created() {
    this.fetchCommonGoods()
  },
  methods: {
    /**
     * 搜索，重置当前页
     */
    fetchCommonGoodsSearch() {
      this.params.currentPage = 1
      this.fetchCommonGoods()
    },
    /**
     * 获取上架状态的普通商品
     */
    fetchCommonGoods() {
      this.params.accessToken = this.token
      apiGoodsMange.apiCLaunchGoods(this.params).then(data => {
        this.goodsList = data.data || {}
      })
    },
    /**
     * 分页
     */
    onChangePage(page) {
      this.params.currentPage = page
      this.fetchCommonGoods()
    },
    /**
     * 展示设置拼单弹框
     */
    onSetLaunchGoods(goodsid, gsp) {
      this.setLaunchPanel = true
      this.addParams.goodsRulesDetailList = JSON.parse(gsp || '[]')
      this.addParams.id = goodsid
    },
    /**
     * 设置拼单规则---提交审核
     */
    submitUpdatedGoodsSkuInfoHandler() {
      let flag = true
      if (!this.addParams.fightGoodsEndTime) {
        this.$toast('请选择拼单结束时间')
        return
      }
      this.addParams.goodsRulesDetailList.forEach(item => {
        if (!item.specpropertyCrowdSum || item.specpropertyCrowdSum == '') {
          this.$toast('请输入颜色最低发货量')
          flag = false
          return
        }
        item.specpropertyList.forEach(spcs => {
          if (!spcs.specpropertySmallCount) {
            this.$toast('请输入采购商最低起订量')
            flag = false
          }
        })
      })
      if (!flag) return
      this.addParams.accessToken = this.token
      apiGoodsMange.apiLaunchGoodsSet(this.addParams).then(data => {
        this.$toast('新增拼单规则成功!', () => {
          this.fetchCommonGoods()
          this.setLaunchPanel = false
          this._cancelParams()
        })
      })
    },
    /**
     * 清空弹框参数
     */
    _cancelParams() {
      this.addParams.fightGoodsEndTime = ''
      this.addParams.crowdfundingCount = ''
      this.addParams.goodsRulesDetailList = []
    }
  }
}
</script>

<style lang="scss">
.m-launch {
  .search-btn {
    .btn {
      width: 140px;
    }
  }
  .table {
    td {
      color: #313131
    }
  }
  .fightSet {
    .w-input .error-tip {
      margin-top: 26px;
      left: 5px;
    }
    .date-list li .message span {
      width: 30px !important
    }
  }
}
</style>

<style lang="scss" scoped>
@import '../../scss/_var.scss';
.t-under {
  text-decoration: underline;
  // margin-right: 18px;
  font-size: 12px;
  color: #3693FF;
  cursor: pointer;
}
.m-launch {
  .title {
    width: 80px;
    text-align: right;
    display: inline-block;
    margin-right: 10px;
    font-size: 12px;
  }
  .icon {
    padding: 0 10px;
  }
  .dis-block {
    display: inline-block;
  }
  .o {
    margin-right: 40px;
    margin-bottom: 20px;
    display: inline-block;
  }
  .select {
    width:142px;
    height:28px; 
  }
  .o-select {
    width: 170px;
    height: 28px;
  }
  .input {
    width: 170px;
    vertical-align: middle;
  }
  .order-options {
    margin: 48px auto 20px;
    text-align: center;
  }
}
.launch-search {
  padding: 30px 20px 20px;
  border-bottom: 1px solid $border-gray-light;
  .search-btn {
    margin-top: 20px;
    text-align: center;
  }
}
.m-goods-list {
  margin: 20px auto 30px;
}
.tab-com {
  margin-bottom: 20px;
}
.table-list {
  margin-bottom: 20px;
  .tab-title {
    width: 100%;
    height:40px; 
    background:rgba(242,242,242,1);
    font-size: 12px;
    color: #313131;
    font-weight: 600;
    line-height: 40px;
    padding-left: 10px;
    border: 1px solid #e0e0e0;
    border-bottom: none;
    & > span {
      &:last-child {
        margin-left: 20px;
      }
    }
  }
  .goods-info {
    padding: 10px;
  }
  .goods-img {
    width: 88px;
    height: 88px;
    margin-right: 14px;
  }
  .goods-name {
    display: inline-block;
    position: relative;
    height: 88px;
    width: 60%;
    span {
      position: absolute;
      bottom: 0;
      left: 0;
    }
  }
  td {
    &:nth-child(1) {
      width: 28%;
    }
    &:nth-child(2) {
      width: 14%;
    }
    &:nth-child(3) {
      width: 12%;
    }
    &:nth-child(4) {
      width: 11%;
    }
    &:nth-child(5) {
      width: 15%;
      span {
        line-height: 24px;
        &:last-child {
          color: #606060
        }
      }
    }
    &:nth-child(6) {
      width: 10%;
    }
    &:nth-child(7) {
      width: 10%;
    }
  }
}
.fightSet {
  // 拼单模态框
  .fightRow {
      margin-bottom: 20px;
    .title {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
    }
    .date {
      display: inline-block;
      width: 352px;
    }
    .radioGroup {
      width: 250px;
      display: inline-block;
      .radio {
        margin-right: 50px;
      }
    }
    .launch-count {
      display: inline-block;
      width: 118px;
      margin-right: 10px;
    }
  }
}
</style>
